package com.wws.family.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.wika.basics.base.BaseDialog
import com.wika.basics.ext.divider
import com.wika.basics.ext.vertical
import com.wika.framework.manager.AppManager
import com.wws.family.R
import com.wws.family.adapter.DialogSelectAdapter
import com.wws.family.databinding.DialogSelectBinding
import com.wws.family.utils.PublicUtils

/**
 *@Description: 选择框
 *@Date: 2024/7/26 16:04
 *@Author: WangWeiShuo
 */
class SelectDialog {
    class Builder(
        context: Context
    ) : BaseDialog.Builder<Builder>(context) {
        private val mBinding: DialogSelectBinding = DialogSelectBinding.inflate(LayoutInflater.from(context))
        private var onSelectListener: OnSelectListener? = null

        init {
            setContentView(mBinding.root)
            val width = AppManager.getInstance(context).getScreenWidthPx()
            val height = AppManager.getInstance(context).getScreenHeightPx()
            setWidth(width)
            setHeight((height * 0.3).toInt())
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            setGravity(Gravity.BOTTOM)
        }

        fun showTitle(title: String?): Builder{
            mBinding.tvSelectTitle.text = title
            return this
        }

        fun setTitleColor(titleColor: Int): Builder{
            mBinding.tvSelectTitle.setTextColor(context.getColor(titleColor))
            return this
        }

        fun showContent(content: MutableList<PublicUtils.MedicineType>?): Builder{
            val dialogSelectAdapter = DialogSelectAdapter(context)
            mBinding.rvSelect.vertical()
            mBinding.rvSelect.divider(color = context.getColor(R.color.gray_27000000), paddingSize = 10, includeLast = false)
            mBinding.rvSelect.adapter = dialogSelectAdapter

            dialogSelectAdapter.setData(content)
            dialogSelectAdapter.onItemClickListener = { view: View, position: Int ->
                if(onSelectListener != null){
                    onSelectListener?.onSelect(position,content!![position].name)
                }
                dismiss()
            }
            return this
        }

        fun addSelectListener(selectListener: OnSelectListener): Builder{
            onSelectListener = selectListener
            return this
        }

        fun showDialog(){
            show()
        }

        fun dismissDialog(){
            dismiss()
        }
    }

    interface OnSelectListener{
        fun onSelect(pos: Int,value: String)
    }
}